<?php

namespace addon\hotel\hotel\controller;


use addon\hotel\model\HotelOrderExport;
use addon\hotel\model\OrderCommon as OrderCommonModel;
use think\facade\Config;

class Order extends BaseHotel
{

    public function lists()
    {
        $order_label_list = array (
            "order_no" => "订单号",
            "out_trade_no" => "外部单号",
            "name" => "姓名",
            "mobile" => "手机号",
        );
        $order_status = input("order_status", "");//订单状态
        $order_name = input("order_name", '');
        $pay_type = input("pay_type", '');

        $start_time = input("start_time", '');
        $end_time = input("end_time", '');
        $settlement_state = input("settlement_state", "");//结算状态
        $order_label = !empty($order_label_list[ input("order_label") ]) ? input("order_label") : "";
        $search_text = input("search", '');

        $order_common_model = new OrderCommonModel();

        if (request()->isAjax()) {
            $page_index = input('page', 1);
            $page_size = input('limit', PAGE_LIST_ROWS);
            $alias = 'o';

            $condition[] = [ 'hotel_id', '=', $this->hotel_id ];
            //订单状态
            if ($order_status != "") {
                $condition[] = [ "order_status", "=", $order_status ];
            }
            //订单内容 模糊查询
            if ($order_name != "") {
                $condition[] = [ "order_name", 'like', "%$order_name%" ];
            }

            //订单支付
            if ($pay_type != "") {
                $condition[] = [ "pay_type", "=", $pay_type ];
            }

            if (!empty($start_time) && empty($end_time)) {
                $condition[] = [ "create_time", ">=", date_to_time($start_time) ];
            } elseif (empty($start_time) && !empty($end_time)) {
                $condition[] = [ "create_time", "<=", date_to_time($end_time) ];
            } elseif (!empty($start_time) && !empty($end_time)) {
                $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
            }
            if ($search_text != "") {
                $condition[] = [ $order_label, 'like', "%$search_text%" ];
            }

            //结算状态
            if ($settlement_state == 1) {
                $condition[] = [ "is_settlement", "=", '1' ];
            } elseif ($settlement_state == 2) {
                $condition[] = [ "is_settlement", "=", '0' ];
            }
            $list = $order_common_model->getOrderPageList($condition, $page_index, $page_size, "create_time desc", $field = 'o.*', $alias);
            return $list;
        } else {
            $this->assign("order_label_list", $order_label_list);
            $order_model = new OrderCommonModel();
            $order_status_list = $order_model->order_status;
            $this->assign("order_status_list", $order_status_list);//订单状态
            //订单来源 (支持端口)
            $order_from = Config::get("app_type");
            $this->assign('order_from_list', $order_from);

            $temp_order_list = array_column($order_model->order_status, "name", "status");
            //$temp_order_list[ 'refunding' ] = '退款中';
            $this->assign('order_status', $temp_order_list);

            $pay_type = $order_common_model->getPayType();
            $this->assign("pay_type_list", $pay_type);

            return $this->fetch('order/lists', [], $this->replace);
        }
    }

    //确认用户入驻
    public function confirmEntry()
    {
        if (request()->isAjax()) {
            $order_id = input("order_id", 0);
            $order_common_model = new OrderCommonModel();
            $close_condition = array (
                [ 'order_id', '=', $order_id ],
                [ 'hotel_id', '=', $this->hotel_id ],
            );

            $result = $order_common_model->orderConfirm($close_condition);
            return $result;
        }
    }

    /**
     * 订单关闭
     * @return mixed
     */
    public function close()
    {
        if (request()->isAjax()) {
            $order_id = input("order_id", 0);
            $order_common_model = new OrderCommonModel();
            $close_condition = array (
                [ 'order_id', '=', $order_id ],
                [ 'hotel_id', '=', $this->hotel_id ],
            );
            $result = $order_common_model->orderClose($close_condition);
            return $result;
        }
    }

    //退款
    public function refund()
    {
        if (request()->isAjax()) {
            $order_id = input("order_id", 0);
            $order_money = input("order_money", 0);
            $refund_type = input("refund_type", 0);
            $refund_money = input("refund_money", 0);
            $is_cancel = input('is_cancel',0);
            /*$total_refund_money	 = input('total_refund_money',0);
            if ($total_refund_money	+ $refund_money>$order_money){
                return $this->error('','错误');
            }*/
            $order_common_model = new OrderCommonModel();
            $close_condition = array (
                [ 'order_id', '=', $order_id ],
                [ 'hotel_id', '=', $this->hotel_id ],
            );

            $data = [
                'order_money'=>$order_money,
                'refund_type'=>$refund_type,
                'refund_money'=>$refund_money,
                'is_cancel'=>$is_cancel
            ];

            /*if ($refund_type==1){
                $data['refund_total_real_money'] = $order_money;
            } else {
                $data['refund_total_real_money'] = $refund_money;
            }*/

            $result = $order_common_model->orderRefund($data,$close_condition);
            return $result;
        }
    }


    //详情
    public function detail()
    {
        $order_id = input("order_id", 0);
        $order_common_model = new OrderCommonModel();
        $condition = array (
            [ 'order_id', '=', $order_id ],
            [ 'hotel_id', '=', $this->hotel_id ]
        );
        $order_detail_result = $order_common_model->getOrderInfo($condition);
        $order_detail = $order_detail_result[ "data" ] ?? [];

        if (empty($order_detail))
            $this->error("查询不到此订单信息!", addon_url('hotel://hotel/order/lists'));

        $this->assign("order_detail", $order_detail);

        return $this->fetch('order/detail', [], $this->replace);
    }

    public function exportorder(){
        $condition_desc = [];
        $order_label_list = array (
            "order_no" => "订单号",
            "out_trade_no" => "外部单号",
            "realname" => "姓名",
            "mobile" => "手机号",
        );
        $order_status = input("order_status", "");//订单状态
        $order_name = input("order_name", '');
        $pay_type = input("pay_type", '');

        $start_time = input("start_time", '');
        $end_time = input("end_time", '');
        $settlement_state = input("settlement_state", "");//结算状态
        $order_label = !empty($order_label_list[ input("order_label") ]) ? input("order_label") : "";
        $search_text = input("search", '');

        $member_info = input("member_info", '');

        $condition = [  [ 'hotel_id', '=', $this->hotel_id ] ];
        //订单状态
        if ($order_status != "") {
            $condition[] = [ "order_status", "=", $order_status ];
        }
        //订单内容 模糊查询
        if ($order_name != "") {
            $condition[] = [ "order_name", 'like', "%$order_name%" ];
        }

        //订单支付
        if ($pay_type != "") {
            $condition[] = [ "pay_type", "=", $pay_type ];
        }

        if (!empty($start_time) && empty($end_time)) {
            $condition[] = [ "create_time", ">=", date_to_time($start_time) ];
        } elseif (empty($start_time) && !empty($end_time)) {
            $condition[] = [ "create_time", "<=", date_to_time($end_time) ];
        } elseif (!empty($start_time) && !empty($end_time)) {
            $condition[] = [ 'create_time', 'between', [ date_to_time($start_time), date_to_time($end_time) ] ];
        }
        if ($search_text != "") {
            $condition[] = [ $order_label, 'like', "%$search_text%" ];
        }


        if ($member_info != "") {
            $condition[] = [ 'nickname|m.mobile', 'like', "%$member_info%" ];
        }

        //结算状态
        if ($settlement_state == 1) {
            $condition[] = [ "is_settlement", "=", '1' ];
        } elseif ($settlement_state == 2) {
            $condition[] = [ "is_settlement", "=", '0' ];
        }
        $order_export_model = new HotelOrderExport();
        $result = $order_export_model->orderExport($condition, $condition_desc, 0, $join ?? null);

        if(!empty($result['data'])){
            $file_path = $result['data']['file'];
            header("Content-type:application/octet-stream");
            $file_name = basename($file_path);
            header("Content-Disposition:attachment;filename = " . $file_name);
            header("Accept-ranges:bytes");
            header("Accept-length:" . filesize($file_path));
            readfile($file_path);
            unlink($file_path);
            exit;
        }else{

        }
    }
}